// import { UserApi } from '@/api/User.api.js'
import UserApi from '@/api/User.api.js'
import { CustomerSalaryApi } from '@/api/CustomerSalary.api.js'
import { accountThead } from './AMtableHead.js'
import _cloneDeep from 'clone-deep'
import CreateAccountModal from './modal/CreateAccountModal.vue'
import BatchImportModal from './modal/BatchImportModal.vue'

export default {
  name: 'AccountModalModal',

  components: {
    CreateAccountModal,
    BatchImportModal
  },

  data () {
    return {
      thead: _cloneDeep(accountThead),
      tableData: [{}],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      formData: {
        name: '',
        userName: '',
        phone: ''
      },
      selectedId: '', // 勾选的id集合
      selectedIdArr: [], // 勾选的id, name数组集合
      isShowCreateAccountModal: false,
      powerType: null
    }
  },

  mounted () {
    console.log('💗💗💗', this.$route.name)
    switch (this.$route.name) {
      case 'customerAccount':
        this.powerType = 1
        break
      case 'employeeAccount':
        this.powerType = 2
        break
      case 'supplierAccount':
        this.powerType = 3
        break
    }
  },

  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 查询
    hasFind () {
      return this.actionArr.indexOf('Find') > -1
    },
    // 查看
    hasView () {
      return this.actionArr.indexOf('View') > -1
    },
    // 导入
    hasImport () {
      return this.actionArr.indexOf('Import') > -1
    },
    // 新增
    hasCreate () {
      return this.actionArr.indexOf('Create') > -1
    },
    // 编辑
    hasEdit () {
      return this.actionArr.indexOf('Edit') > -1
    },
    // 删除
    hasDelete () {
      return this.actionArr.indexOf('Delete') > -1
    },
    // 启用
    hasEnabledList () {
      return this.actionArr.indexOf('EnabledList') > -1
    },
    // 禁用
    hasDisEnabledList () {
      return this.actionArr.indexOf('DisEnabledList') > -1
    }
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        GetRoleName: true,
        Name: this.formData.name,
        UserName: this.formData.userName,
        Phone: this.formData.phone,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize,
        PowerType: this.powerType
      }
      let res = await UserApi.getList(params)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedIdArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      let arr = value.map((item) => {
        return item.id
      })
      this.selectedId = arr.join(',')
      // console.log('列表勾选数组', this.selectedIdArr)
      // console.log('列表勾选数组字符串', this.selectedId)
    },

    // 显示批量导入弹窗
    handleShowBatchImportModal () {
      this.$refs.BatchImportModal.file = null
      this.$refs.BatchImportModal.errorData.isError = false
      this.$refs.BatchImportModal.isShowBatchImportModal = true
    },

    // 显示新增弹窗
    handleShowCreateAccountModal (id) {
      this.$refs.CreateAccountModal.show()
      this.$refs.CreateAccountModal.isShowCreateAccountModal = true
      this.$refs.CreateAccountModal.createAccountModalTitle = id ? '修改账号' : '新增账号'
      this.$refs.CreateAccountModal.basicDataId = id
      this.$refs.CreateAccountModal.getModel(id)
      // if (!id) {
      //   console.log('xxxxxxxxxxxxx')
      //   this.$refs.CreateAccountModal.formData.enabled = true
      // }
    },

    // 启用/禁用
    async enabledList (id, enabled) {
      var params = {
        ids: id,
        enabled: enabled
      }
      let res = await UserApi.enabledList(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.selectedId = []
      this.selectedIdArr = []
    },

    // 批量删除
    async handleDel (selectedId) {
      var params = {
        id: selectedId
      }
      let res = await UserApi.delete(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    handleMaxSizeTen () {
      this.$Message.error({
        content: '您上传的文件太大，上传文件大小不能超过10M',
        duration: 10,
        closable: true
      })
    },

    // 是否创建VXCore账户前询问
    isCreateVXCodeModal (id) {
      this.$Modal.confirm({
        title: '是否创建VXCore账户？',
        onOk: () => {
          this.handelCreateAccountForVX(id)
        }
      })
    },

    // 创建VXCore账户
    async handelCreateAccountForVX (id) {
      var params = {
        userId: id
      }
      let res = await UserApi.createAccountForVX(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.query()
    }
  }
}
